import 'dart:async';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// 该类内容请不要随意编辑. 会影响app的文字显示.
class I18n implements WidgetsLocalizations {
  const I18n();

  ///生成对应的语言包.
  static I18n current;

  /// 国际化委托协议.
  static const GeneratedLocalizationsDelegate delegate =
      GeneratedLocalizationsDelegate();

  //为了更快速的使用I18n..其实没有这个好像也没关系.还可以通过I18().sort / I18n.current 调用国际化 .但是为什么呢?
  static I18n of(BuildContext context) => Localizations.of<I18n>(context, I18n);

  ///文本方向. WidgetsLocalizations必须要实现的抽象类方法之一.
  @override
  TextDirection get textDirection => TextDirection.ltr;

  // 默认的语言包.
  String get sort => "分類";
  String get videoCource => "視頻課程";
  String get company => "企業商會";
  String get community => "社區活動";
  String get volunteer => "誌願招募";
  String get shareArticle => "分享文章";
  String get historyArticle => "歷史文章";
  String get allCourses => "所有課程";
  String get hotCourse => "課程推薦";
  String get more => "更多";
  String get discovery => "發現";
  String get circle => "成長圈";
  String get enterpriseService => "誠信商圈";
  String get me => "我的";
  String get login => "請登錄";
  String get fans => "粉絲";
  String get follow => "關註";
  String get learningMileage => "學習裏程";
  String get shengdianSupport => "盛點支持";
  String get systemMessage => "系統消息";
  String get search => "搜索";
  String get alarm => "鬧鐘";
  String get changePassword => "修改密碼";
  String get pushNotification => "推送通知";
  String get selectCity => "選擇城市";
  String get version => "版本";
  String get setting => "設置";
  String get certification => "學員認證";
  String get font => '繁体中文';
  String get scan => '扫一扫';
  String get qrCode => '我的二维码';
  String get xianggan => '香港';
  String get guangzhou => '广州';
  String get taibei => '台北';
  String get kunming => '昆明';
  String get aomen => '澳门';
  String get foshan => '佛山';
  String get xiamen => '厦门';
  String get dongguang => '东莞';
  String get shenzhen => '深圳';
  String get zhongshan => '中山';
  String get wuhan => '武汉';
  String get searchContent => '搜索内容';
  String get cancel => '取消';
  String get viewStateButtonError => "加载失败";
  String get viewStateButtonRetry => "重试";
  String get viewStateMessageEmpty => "没有发现更多";
  String get viewStateButtonRefresh => "刷新";
  String get myTest => "我的彩析";

}

///$代表全局类. 默认的语言包. 若手机设置的语言在i18n没有,, 则会使用这里的语言包.
class $tw extends I18n {
  const $tw();
}
/// 中国简体中文语言包
class $cn extends I18n {
  const $cn();

  @override
  TextDirection get textDirection => TextDirection.ltr;
  @override
  String get sort => "分类";
  @override
  String get videoCource => "视频课程";
  @override
  String get company => "企业商会";
  @override
  String get community => "社区活动";
  @override
  String get volunteer => "志愿招募";
  @override
  String get shareArticle => "分享文章";
  @override
  String get historyArticle => "历史文章";
  @override
  String get allCourses => "所有课程";
  @override
  String get hotCourse => "课程推荐";
  @override
  String get more => "更多";
  @override
  String get discovery => "发现";
  @override
  String get circle => "成长圈";
  @override
  String get enterpriseService => "诚信商圈";
  @override
  String get me => "我的";
  @override
  String get login => "请登录";
  @override
  String get fans => "粉丝";
  @override
  String get follow => "关注";
  @override
  String get learningMileage => "学习里程";
  @override
  String get shengdianSupport => "盛点支持";
  @override
  String get systemMessage => "系统消息";
  @override
  String get search => "搜索";
  @override
  String get alarm => "闹钟";
  @override
  String get changePassword => "修改密码";
  @override
  String get pushNotification => "推送通知";
  @override
  String get selectCity => "选择城市";
  @override
  String get version => "版本";
  @override
  String get setting => "设置";
  @override
  String get certification => "学员认证";
  @override
  String get font => '简体中文';
  @override
  String get scan => '扫一扫';
  @override
  String get qrCode => '我的二维码';
  @override
  String get xianggan => '香港';
  @override
  String get guangzhou => '廣州';
  @override
  String get taibei => '台北';
  @override
  String get kunming => '昆明';
  @override
  String get aomen => '澳門';
  @override
  String get foshan => '佛山';
  @override
  String get xiamen => '廈門';
  @override
  String get dongguang => '東莞';
  @override
  String get shenzhen => '深圳';
  @override
  String get zhongshan => '中山';
  @override
  String get wuhan => '武漢';
  @override
  String get searchContent => '搜索内容';
  @override
  String get cancel => '取消';
  @override
  String get viewStateButtonError => "加載失敗";
  @override
  String get viewStateButtonRetry => "重試";
  @override
  String get viewStateMessageEmpty => "没有发现更多";
  @override
  String get viewStateButtonRefresh => "刷新";
  @override
  String get myTest => "我的彩析";

}

/// 国际化协议, 必须实现3个抽象类方法.
class GeneratedLocalizationsDelegate extends LocalizationsDelegate<I18n> {
  const GeneratedLocalizationsDelegate();

  ///支持的语言环境.
  List<Locale> get supportedLocales {
    return const <Locale>[
//      地域, 语言, 不能随便乱写, 要符合各国语言各国语言简写,不然会报错.
        Locale('zh','TW'),
        Locale('zh','CN'),
    ];
  }

  ///这个方法会获取手机设置的语言,,判断后加载对应语言的语言包.
  @override
  Future<I18n> load(Locale locale) {
    final String lang = getLang(locale);
    if (lang != null) {
      switch (lang) {
        case "zh_TW":
          I18n.current = const $tw();
          return SynchronousFuture<I18n>(I18n.current);
        case "zh_CN":
          I18n.current = const $cn();
          return SynchronousFuture<I18n>(I18n.current);
        default:
        // NO-OP.
      }
    }
    //若没有对应的语言包,则显示默认的那个语言包.
    I18n.current = const I18n();
    return SynchronousFuture<I18n>(I18n.current);
  }

  ///区域回调数组: 当传入的是不支持的语种，可以根据这个回调，返回相近,并且支持的语种.
  LocaleListResolutionCallback listResolution(
      {Locale fallback, bool withCountry = true}) {
    return (List<Locale> locales, Iterable<Locale> supported) {
      if (locales == null || locales.isEmpty) {
        return fallback ?? supported.first;
      } else {
        return _resolve(locales.first, fallback, supported, withCountry);
      }
    };
  }

  ///区域回调: 当传入的是不支持的语种，可以根据这个回调，返回相近,并且支持的语种.
  LocaleResolutionCallback resolution(
      {Locale fallback, bool withCountry = true}) {
    return (Locale locale, Iterable<Locale> supported) {
      return _resolve(locale, fallback, supported, withCountry);
    };
  }

  ///加载指定语言环境,如果加载了load实例,则返回true
  @override
  bool isSupported(Locale locale) => _isSupported(locale, true);

  ///加载这个委托的资源,则返回false, 再次调用[load]方法.
  @override
  bool shouldReload(GeneratedLocalizationsDelegate old) => false;

  ///从语言环境列表解析语言环境的内部方法。
  Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported,
      bool withCountry) {
    if (locale == null || !_isSupported(locale, withCountry)) {
      return fallback ?? supported.first;
    }

    final Locale languageLocale = Locale(locale.languageCode, "");
    if (supported.contains(locale)) {
      return locale;
    } else if (supported.contains(languageLocale)) {
      return languageLocale;
    } else {
      final Locale fallbackLocale = fallback ?? supported.first;
      return fallbackLocale;
    }
  }

  ///
  ///如果支持指定的语言环境，则返回true，否则返回false。
  ///
  bool _isSupported(Locale locale, bool withCountry) {
    if (locale != null) {
      for (Locale supportedLocale in supportedLocales) {
        // Language must always match both locales.
        if (supportedLocale.languageCode != locale.languageCode) {
          continue;
        }

        // If country code matches, return this locale.
        if (supportedLocale.countryCode == locale.countryCode) {
          return true;
        }

        // If no country requirement is requested, check if this locale has no country.
        if (true != withCountry &&
            (supportedLocale.countryCode == null ||
                supportedLocale.countryCode.isEmpty)) {
          return true;
        }
      }
    }
    return false;
  }

}

///如果locale为null则返回null, 不为null国家代码不为null和地区不为空,则返回传入的语言,否则返回默认的语言.
String getLang(Locale l) => l == null
    ? null
    : l.countryCode != null && l.countryCode.isEmpty
    ? l.languageCode
    : l.toString();